package org.jeecg.eqp.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.eqp.entity.EqpEqpchkCfg;
import org.jeecg.eqp.service.IEqpEqpchkCfgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.CudUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @Description: 设备位置主数据
 * @Author: licy
 * @Date: 2022-10-12
 * @Version: V1.0
 */
@Api(tags = "设备位置主数据")
@RestController
@RequestMapping("/eqp/eqpEqpchkCfg")
@Slf4j
public class EqpEqpchkCfgController extends JeecgController<EqpEqpchkCfg, IEqpEqpchkCfgService> {
	@Autowired
	private IEqpEqpchkCfgService eqpEqpchkCfgService;
	@Lazy
	@Resource
	private CommonAPI commonApi;

	/**
	 * 单表分页列表查询
	 *
	 * @param eqpEqpchkCfg
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "设备位置主数据-分页列表查询")
	@ApiOperation(value = "设备位置主数据-分页列表查询", notes = "设备位置主数据-分页列表查询")
	@GetMapping(value = "/list")
	@DS("history")
	public Result<IPage<EqpEqpchkCfg>> queryPageList(EqpEqpchkCfg eqpEqpchkCfg,
													 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
													 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
													 HttpServletRequest req) {
		QueryWrapper<EqpEqpchkCfg> queryWrapper = QueryGenerator.initQueryWrapper(eqpEqpchkCfg, req.getParameterMap());
		Page<EqpEqpchkCfg> page = new Page<EqpEqpchkCfg>(pageNo, pageSize);
		IPage<EqpEqpchkCfg> pageList = eqpEqpchkCfgService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

    /**
     * 添加
     *
     * @param eqpEqpchkCfg
     * @return
     */
    @AutoLog(value = "设备位置主数据-添加")
    @ApiOperation(value = "设备位置主数据-添加", notes = "设备位置主数据-添加")
    @PostMapping(value = "/add")
    @DS("business")
    public Result<String> add(@RequestBody EqpEqpchkCfg eqpEqpchkCfg) throws Exception {
        Integer counterValue = commonApi.getCounterValue("recid.eqp_eqpchk_cfg");
        eqpEqpchkCfg.setRecId(counterValue);
        eqpEqpchkCfg.setCtime(new Date());
		eqpEqpchkCfgService.save(eqpEqpchkCfg);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param eqpEqpchkCfg
     * @return
     */
    @AutoLog(value = "设备位置主数据-编辑")
    @ApiOperation(value = "设备位置主数据-编辑", notes = "设备位置主数据-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    @DS("business")
    public Result<String> edit(@RequestBody EqpEqpchkCfg eqpEqpchkCfg) throws Exception {
        //框架修改 eqpEqpchkCfgService.updateById(eqpEqpchkCfg);
        //return Result.OK("编辑成功!");
        eqpEqpchkCfg.setCtime(new Date());
		eqpEqpchkCfgService.updateById(eqpEqpchkCfg);
        return Result.OK("修改成功！");
    }

    /**
     * 通过map删除
     *
     * @param map
     * @return
     */
    @AutoLog(value = "设备位置主数据-通过id删除")
    @ApiOperation(value = "设备位置主数据-通过id删除", notes = "设备位置主数据-通过id删除")
    @DeleteMapping(value = "/delete")
    @DS("business")
    public Result<String> delete(EqpEqpchkCfg eqpEqpchkCfg) throws Exception {
		eqpEqpchkCfgService.removeById(eqpEqpchkCfg);
        return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备位置主数据-批量删除")
	@ApiOperation(value = "设备位置主数据-批量删除", notes = "设备位置主数据-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	@DS("business")
	public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.eqpEqpchkCfgService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "设备位置主数据-通过id查询")
	@ApiOperation(value = "设备位置主数据-通过id查询", notes = "设备位置主数据-通过id查询")
	@GetMapping(value = "/queryById")
	@DS("business")
	public Result<EqpEqpchkCfg> queryById(@RequestParam(name = "id", required = true) String id) {
		EqpEqpchkCfg eqpEqpchkCfg = eqpEqpchkCfgService.getById(id);
		if (eqpEqpchkCfg == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(eqpEqpchkCfg);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param eqpEqpchkCfg
	 */
	@RequestMapping(value = "/exportXls")
	@DS("history")
	public ModelAndView exportXls(HttpServletRequest request, EqpEqpchkCfg eqpEqpchkCfg) {
		return super.exportXls(request, eqpEqpchkCfg, EqpEqpchkCfg.class, "设备位置主数据");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	@DS("business")
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, EqpEqpchkCfg.class);
	}

}
